package com.xps.and.yuntong.Data.model;

import android.content.Context;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;
import com.xps.and.yuntong.R;

import java.util.List;

public class MyTraceInfo {
    private Overlay mStartMarker;
    private Overlay mCurrentMarker;
    private Overlay mEndMarker;

    private Polyline mVirtureRoad;
    private BaiduMap mBaiduMap;
    private Context mContext;
    PolylineOptions polylineOptions;

    public MyTraceInfo(Context context, BaiduMap baiduMap) {
        this.mBaiduMap = baiduMap;
        this.mContext = context;
        polylineOptions = new PolylineOptions();

    }

    public void updatePolyline(List<LatLng> latlngs) {
        if (latlngs == null) return;
        if (mVirtureRoad != null) {
            mVirtureRoad.remove();
            mVirtureRoad = null;
        }
        int color = mContext.getResources().getColor(R.color.colorPrimary);

        if (latlngs.size() > 2) {
            polylineOptions.points(latlngs).width(10).color(color);
            mVirtureRoad = (Polyline) mBaiduMap.addOverlay(polylineOptions);
        }

    }

    public void updateStartMarker(LatLng latlng) {
        if (mStartMarker != null) {
            mStartMarker.remove();
            mStartMarker  = null;
        }
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.mipmap.s_s);
        MarkerOptions option = new MarkerOptions().position(latlng)
                .icon(bitmap);

        mStartMarker = mBaiduMap.addOverlay(option);

    }

    /**
     * 当前位置marker
     */
    public void updateCurrentMarker(LatLng latlng) {
        if (mCurrentMarker != null) {
            mCurrentMarker.remove();
            mCurrentMarker = null;
        }
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.mipmap.s_c);
        OverlayOptions option = new MarkerOptions()
                .position(latlng)
                .anchor(0.5f, 0.5f)
                .icon(bitmap);
        mCurrentMarker = mBaiduMap.addOverlay(option);
    }

    /**
     * 终点marker
     */
    public void updateEndMarker(LatLng latlng) {
        if (mEndMarker != null) {
            mEndMarker.remove();
            mEndMarker = null;
        }
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.mipmap.s_e);
        OverlayOptions option = new MarkerOptions()
                .position(latlng)
                .icon(bitmap);
        mEndMarker = mBaiduMap.addOverlay(option);
    }


}
